package com.qst.yunpan.service.impl;

import com.qst.yunpan.dao.UserMapper;
import com.qst.yunpan.entity.User;
import com.qst.yunpan.entity.UserExample;
import com.qst.yunpan.entity.UserKey;
import com.qst.yunpan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapperDao;

    @Override
    public User login(String username, String password) {
        UserExample usersExample = new UserExample();
        usersExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> list =userMapperDao.selectByExample(usersExample);
        if(!list.isEmpty()){
            User user = list.get(0);
            return user;
        }else {
            return null;
        }
    }

    @Override
    public boolean register(User user) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> list = userMapperDao.selectByExample(userExample);
        if(list.isEmpty()){
            //没与相同的用户名，注册成功
            userMapperDao.insert(user);
            return true;
        }else{
            //有相同的用户名，注册失败
            return false;
        }

    }

    @Override
    public String getCountSize(String username) {
        String counSize = null;
        try {
            counSize = userMapperDao.getCountsize(username);
        } catch (Exception e) {
            e.printStackTrace();
            return counSize;
        }
        return counSize;
    }


}
